﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using RentIt06WPFClient.UserSession;

namespace RentIt06WPFClient
{
    /// <summary>
    /// Interaction logic for MediaView.xaml
    /// </summary>
    public partial class MediaView : Page
    {
        private const string NotForRentalString = "Not available for rental.";
        private const string NotForPurchaseString = "Not available for purchase.";
        private const string OwnershipString = "You own this product and are free to download it!";
        private const string RentalString =
            "You have currently rented this product. Download it now!";

        private Media media;

        public MediaView(Media m) {
            InitializeComponent();
            media = m;
            Content.DataContext = media;
            if (!media.CanBeRented) {
                RentalButton.IsEnabled = false;
                RentalText.Text = NotForRentalString;
            }
            if (!media.CanBeBought) {
                PurchaseButton.IsEnabled = false;
                PurchaseText.Text = NotForPurchaseString;
            }

            ReapplyState();
        }

        private void ReapplyState() {
            // If the media is available for download (meaning it has either been rented or bought).
            if (!(from m in Session.User.ReadDownloadableProducts() where m.Id == this.media.Id select m).Any()) 
                return;
            this.RentalPanel.Visibility = Visibility.Hidden;
            this.DownloadPanel.Visibility = Visibility.Visible;
            // The purchase associated with the media.
            var purchase = (from p in Session.User.ReadPreviousPurchases()
                            where p.Product.Id == this.media.Id select p).First();
            // If the purchase has an expiry date of MaxValue (meaning it has been bought).
            if (purchase.Expires.Year == DateTime.MaxValue.Year) {
                this.OwnershipText.Text = OwnershipString;
            } else { // Else it has been rented.
                this.OwnershipText.Text = RentalString;
                this.ExpiresCaption.Visibility = Visibility.Visible;
                this.ExpiresDate.Text = purchase.Expires.ToString();
            }
        }

        private void RentalButton_Click(object sender, RoutedEventArgs e) {
            var myCredits = Session.User.ReadMyAccount().Credit;
            if (myCredits >= media.RentingPrice) {
                Session.User.RentProduct(media.Id);
                ReapplyState();
            } else {
                var addCreditWindow = new AddCredit(media.RentingPrice - myCredits);
                addCreditWindow.Closed += addCreditWindow_Closed;
            }
        }

        private void addCreditWindow_Closed(object sender, EventArgs e) {
            Page_Loaded(null,null);
        }

        private void PurchaseButton_Click(object sender, RoutedEventArgs e) {
            var myCredits = Session.User.ReadMyAccount().Credit;
            if (myCredits >= media.PurchasePrice) {
                Session.User.BuyProduct(media.Id);
                ReapplyState();
            } else {
                var addCreditWindow = new AddCredit(media.PurchasePrice - myCredits);
                addCreditWindow.Closed += addCreditWindow_Closed;
            }
        }

        private void DownloadButton_Click(object sender, RoutedEventArgs e) {
            var window = new Download(media);
            window.Show();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e) {
            //Code that needs to be run on refresh goes here
            var balance = String.Format("${0:0.00}", Session.User.ReadMyAccount().Credit);
            BalanceText.Text = balance;
        }
    }
}
